package com.example.service;


import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Animal;
import com.example.entity.User;
import com.example.mapper.AnimalMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 宠物信息业务层
 */
@Service
public class AnimalService {
    @Resource
    private AnimalMapper animalMapper;

    public Animal selectById(Integer id) {
        return animalMapper.selectById(id);
    }

    public void add(Animal animal) {
        //进行逻辑处理
            animalMapper.insert(animal);
        }
    public void updateState(Integer id, Integer state) {
        Animal animal = selectById(id);
        if (animal != null) {
            // 更新用户的宠物 ID
            animal.setState(state);
            updateById(animal); // 调用更新用户的方法
        }
    }
    public PageInfo<Animal> selectPage(Animal animal, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Animal> list;
        if(ObjectUtil.isNotEmpty(animal.getName())){
            list = animalMapper.selectByName(animal.getName());
        } else {
            list = animalMapper.selectAll();
        }

        return PageInfo.of(list);

    }

    public void updateById(Animal animal) {
        animalMapper.updateById(animal);
    }

    public void deleteById(Integer id) {
        animalMapper.deleteById(id);
    }

    public List<Animal> selectAll() {
        return animalMapper.selectAll();
    }




}

